/**
 * 图文消息
 * @param art
 * @returns
 */
function WechatText(art){
	this.title = art.title;
	this.description = art.description;
	this.picUrl = art.picUrl;
	this.url = art.url;
}
function WechatText(){}
module.exports = WechatText;

/**
 * 拼装图文消息
 * @param postMsg
 * @param articles
 * @returns {string}
 */
WechatText.prototype.buildPicTextMsg = function(postMsg, articles){
    var xml = '';
        xml = "<xml>";
        xml += "<ToUserName>"+postMsg.fromUserName+"</ToUserName>";
        xml += "<FromUserName>"+postMsg.toUserName+"</FromUserName>";
        xml += "<CreateTime>"+new Date().getTime()+"</CreateTime>";
        xml += "<MsgType>news</MsgType>";
        xml += "<ArticleCount>"+articles.length+"</ArticleCount>";
        xml += "<Articles>";
        for(var i=0 ; i<articles.length; i++){
            var art = articles[i];
            if(i <= 8){
                xml += "<item>";
                xml += "<Title>"+art.title+"</Title>";
                xml += "<Description>"+art.descr+"</Description>";
                xml += "<PicUrl>"+art.img+"</PicUrl>";
                xml += "<Url>"+art.url+"</Url>";
                xml += "</item>";
            }else{
                xml += "<item>";
                xml += "<Title>点击查看更多公告话题</Title>";
                xml += "<Description></Description>";
                xml += "<PicUrl></PicUrl>";
                xml += "<Url>"+config.weiChat_url+"noticelist</Url>";
                xml += "</item>";
            }
        }
        xml += "</Articles>";
        xml += "<FuncFlag>1</FuncFlag>";
        xml += "</xml>";
    return xml;
}

/**
 * 得到一个纯文本消息
 * @postMsg 
 * @content
 */
WechatText.prototype.getTextMsg = function getTextMsg(postMsg, content){
	var xml = "<xml>";
		xml += "<ToUserName><![CDATA["+postMsg.fromUserName+"]]></ToUserName>";
		xml += "<FromUserName><![CDATA["+postMsg.toUserName+"]]></FromUserName>";
		xml += "<CreateTime>"+new Date().getTime()+"</CreateTime>";
		xml += "<MsgType><![CDATA[text]]></MsgType>";
		xml += "<Content><![CDATA["+content+"]]></Content>";
		xml += "<FuncFlag>0</FuncFlag>";
		xml += "</xml>";
	return xml;
}

WechatText.prototype.contactCustomService = function contactCustomService(postMsg) {
    var xml = "<xml>";
        xml += "<ToUserName><![CDATA["+postMsg.fromUserName+"]]></ToUserName>";
        xml += "<FromUserName><![CDATA["+postMsg.toUserName+"]]></FromUserName>";
        xml += "<CreateTime>"+new Date().getTime()+"</CreateTime>";
        xml += "<MsgType><![CDATA[transfer_customer_service]]></MsgType>";
        xml += "</xml>";
    return xml;
}

/**
 * 得到一个图文消息
 * @postMsg
 * @url
 * @img
 * @title
 */
WechatText.prototype.getPicTextMsg = function getPicTextMsg(postMsg, url, img, title, descr){
	var xml = '';
		xml = "<xml>";
		xml += "<ToUserName>"+postMsg.fromUserName+"</ToUserName>";
		xml += "<FromUserName>"+postMsg.toUserName+"</FromUserName>";
		xml += "<CreateTime>"+new Date().getTime()+"</CreateTime>";
		xml += "<MsgType>news</MsgType>";
		xml += "<ArticleCount>1</ArticleCount>";
		xml += "<Articles>";
			xml += "<item>";
			xml += "<Title>"+title+"</Title>";
			xml += "<Description>"+descr+"</Description>";
			xml += "<PicUrl>"+img+"</PicUrl>";
			xml += "<Url>"+url+"</Url>";
			xml += "</item>";
		xml += "</Articles>";
		xml += "<FuncFlag>1</FuncFlag>";
		xml += "</xml>";
		return xml;
}